﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Helpers;
using Website.Infrastructure;

namespace Website.Tests
{
    [TestClass]
    public class ImageManagerTests
    {
        const string siteDefinitionFilename = "sitedefinition.xml";

        [TestMethod]
        public void SaveEmptyPostedFileTest()
        {
            // Arrange
            var postedFile = new Mock<HttpPostedFileBase>();
            postedFile.Setup(f => f.ContentLength).Returns(0);

            // Act
            var imageManager = new ImageManager();
            string result = imageManager.SavePostedFile(postedFile.Object);

            // Assert
            Assert.AreEqual(result, string.Empty);
        }

        [TestMethod]
        public void SavePostedFileTest()
        {
            // Arrange
            var postedFile = new Mock<HttpPostedFileBase>();
            postedFile.Setup(f => f.ContentLength).Returns(1024);
            postedFile.Setup(f => f.FileName).Returns(siteDefinitionFilename);
            postedFile.Setup(f => f.SaveAs(It.IsAny<string>()));

            // Act
            var imageManager = new ImageManager();
            string result = imageManager.SavePostedFile(postedFile.Object);

            // Assert
            Assert.AreEqual(result, siteDefinitionFilename);
        }

        [TestMethod]
        public void SaveThumbnailTest()
        {
            const string imageFilename = "testfile.jpg";

            var cwd = Directory.GetCurrentDirectory();
            var resizeFilename = Path.Combine(Path.Combine(cwd, "images"), imageFilename);
            var thumbFilename = Path.Combine(Path.Combine(cwd, "thumbs"), imageFilename);

            // Arrange
            var imageManager = new Mock<ImageManager>();
            imageManager.Setup(m => m.ResizeImage(resizeFilename, 128, 128));
            imageManager.Setup(m => m.SaveImage(thumbFilename, It.IsAny<WebImage>()));

            // Act
            imageManager.Object.SaveThumbnail(imageFilename);

            // Assert
            imageManager.VerifyAll();
        }
    }
}
